
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

int size_numbers_entered;

/* _____________________________________________________________________________________________________________________*/

void display(int a[])
{

	int i = 0;

	printf("[");

	for (i = 0; i < size_numbers_entered; i++)
		printf("%d ", a[i]);

	printf("] \n");

}

/* _____________________________________________________________________________________________________________________*/

void check_value_in_list(int a[])
{
	int i, x;

	printf("Input the number, for which you want to check in the list \t");
	scanf("%d", &x);
	for (i = 0; i < size_numbers_entered; i++) ;
	{
		if (a[i] == x) {
			printf(" The number you entered exists in the list\n ");

		} else {
			return;
		}
	}

}

/* _____________________________________________________________________________________________________________________*/

void reverse(int a[])
{
	int i;

	printf("The original list printed \n");
	display(a);
	printf(" \n \nThe list printed in reverse order: \n ");
	printf("[");
	for (i = size_numbers_entered - 1; i >= 0; i--) {
		printf(" %d", a[i]);
	}
	printf("]");
}

/* _____________________________________________________________________________________________________________________*/

void insert(int a[])
{
	int x, k, l,b[100], i, j;

	printf("Enter the number you want to insert and the index with a comma in between \t ");
	scanf("%d,%d", &x, &l);

	
	for (i = 0; i < size_numbers_entered; i++) {
		b[i] = a[i];
	}
	
	a[l] = x;
	size_numbers_entered++;
	a = realloc(a, sizeof(int) * size_numbers_entered);

	for (j = l + 1; j < size_numbers_entered; j++) {
		a[j] = b[j - 1];
	}

	display(a);

}

// This code is to use the insert function with inplace insertion. 

/*
	int i, x, k, temp, temp1;

	printf("Enter the number you want to insert and the index with a comma in between \n ");
	scanf("%d,%d", &x, &i);

	temp1 = a[size_numbers_entered-1];              
	for (k = i; k < size_numbers_entered; k++) {                  
		temp = a[i + 1];
		a[i + 1] = a[i];
	}
	a[i] = x;
	a[size_numbers_entered] = temp1;

	printf(" You inserted %d at %d position \n", x, i);

	printf("[");

	for (k = 0; k < size_numbers_entered + 1; k++) {
		printf(" %d", a[i]);
	}
	printf("]");

*/

/* _____________________________________________________________________________________________________________________*/

void indexx(int a[])
{
	int x, i, k, index_x;

	printf("Enter the number for which you want the index \n ");
	scanf("%d", &x);

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x) {
			break;
		}

		index_x = i;
	}

	printf(" \n Index of %d is %d \n", x, index_x);

}

/* _____________________________________________________________________________________________________________________*/

void list_size(int a[])
{
	printf(" The size of the list is %d \n ", size_numbers_entered);
}

/* _____________________________________________________________________________________________________________________*/

void count(int a[])
{
	int i, count = 0, x;

	printf("Enter the number whose frequency you want in the list \t ");
	scanf("%d", &x);

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x)
			count++;
	}

	printf(" The frequency of occurence of  \t %d is \t %d \n ", x, count);
}

/* _____________________________________________________________________________________________________________________*/

void sort(int a[])
{

//  Soring using bubble sort

	int i, j, temp;

	for (i = 0; i < size_numbers_entered; i++) {

		for (j = 0; j < size_numbers_entered; j++) {
			if (a[j] > a[i]) {
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
	printf(" The sorted list is : \n ");
	printf("[");
	
	        for (i = 0; i < size_numbers_entered; i++) {
		        printf(" %d", a[i]);
	        }
	
	printf("]");

}

/* _____________________________________________________________________________________________________________________*/
int pop(int a[])
{
	int remove_x, k, index, i, return_value;

	printf("\nEnter the index of the number to be removed \t");
	scanf("%d", &index);

	return_value = a[index];
	
	
        if(index > size_numbers_entered)
        {  return; }
        
	for (i = index + 1; i < size_numbers_entered; i++) {
		a[i - 1] = a[i];
	}
	printf("You popped %d \n", return_value);
	printf("Your list is now\n");
	size_numbers_entered--;
	
	
	
	display(a);
        return return_value;
}

/* _____________________________________________________________________________________________________________________*/

void removefunc(int a[])
{

	int x, remove_x, i, j, k, count;

	printf("\nEnter the number to be removed\t");
	scanf("%d", &x);
	count = 0;

	remove_x = -1;

	for (i = 0; i < size_numbers_entered; i++) {
		if (a[i] == x) {
			remove_x = i;
			break;
		}
	}
	if (remove_x != -1) {
		for (j = remove_x; j < size_numbers_entered - 1; j++) {
			a[j] = a[j + 1];
		}
	}
	size_numbers_entered--;

	display(a);

}

/* _____________________________________________________________________________________________________________________*/

add_values(int a[])
{
	int num, i, number;

	printf("How many more numbers do you want to enter");
	scanf("%d", &num);
	size_numbers_entered += num;
	a = realloc(a, sizeof(int) * (size_numbers_entered));

	for (i = size_numbers_entered - num; i < size_numbers_entered; i++) {

		printf("Enter a number \t \n");
		scanf("%d", &number);
		a[i] = number;
	}
	printf("Your list is now \t ");
	display(a);

}

/* _____________________________________________________________________________________________________________________*/

void extend(int a[])
{

	int appendvalue, i,*b, x, number, n;
	

	printf(" \n This function extends your current list by concatenating a list or number \t \n");
	add_values(a);
}

/* _____________________________________________________________________________________________________________________*/

void append(int a[])
{
	int i, appendvalue;

	printf(" \n Enter value to append \t ");
	scanf("%d", &appendvalue);
	size_numbers_entered++;
	
	a = realloc(a, sizeof(int) * size_numbers_entered);
	a[size_numbers_entered - 1] = appendvalue;
	
	display(a);
}

/* _____________________________________________________________________________________________________________________*/

/* **************************************************************************************************************** */

int main()
{
	int *a;
	int option = 1, number, i = 0, counter;
	char string[10], string2[10];

	printf("\\WELCOME TO PYTHON LIST IMPLEMENTATION/    \n");

	/* This code is an alternative to the code written below . In this the user  will not be asked for an input , that is how many numbers do you want to enter
	
	   a=malloc(16);
	   int num;
	   char cont;

	   do{ 
	   printf("Please enter an number");                       
	   
	   
	 scanf("%d",&num);

	   a[i] = num;
	   i++;

	   printf("Do you want to continue");           
	   scanf("%c",cont);
	   }
	   while(strcmp(cont,'y')==0);

	   size_numbers_entered=i;

	 */

	// code for user input

	printf("Enter how many numbers do you want to enter ");
	scanf("%d", &size_numbers_entered);
	a = malloc(sizeof(int) * size_numbers_entered);	        //Implementation by dynamic allocation

	//int a[size_numbers_entered+5];    The Code was implemented by static allocation earlier . This statement will be used for static        implementation

	for (i = 0; i < size_numbers_entered; i++) {
		printf("Enter an number ");
		scanf("%d", &number);
		a[i] = number;

	}
	printf("\t\t\t\tYour list is \n\n");

	display(a);

	printf("\t\t\t\tYour list has  %d elements \n\n", size_numbers_entered);

	do {
		printf(" Which function do you want to implement - \n ");
		printf
		    (" \n------------------------------------------------------------------------------------------ \n");
		printf("       	           0)        Display\n");
		printf("                   1)        Append\n");
		printf("                   2)        Extend\n");
		printf("                   3)        Pop\n");
		printf("                   4)        Remove\n");
		printf("                   5)        Index\n");
		printf("                   6)        Insert\n");
		printf("                   7)        list_size\n");
		printf("                   8)        Sort\n");
		printf("                   9)        Reverse\n");
		printf("                   10        Count\n");
		printf("                   11)       Check if value in list\n");
		printf("                   12)       Add more values\n");
		printf("                   13)       Exit\n");

		printf
		    (" \n    ------------------------------------------------------------------------------------------   \n  ");

		printf("\t\t\t\t Please enter an option \t");
		scanf("%d", &option);

		//code for choosing which function  

		switch (option) {
		case 0:
			display(a);
			
			break;
		case 1:
			append(a);
			break;
		case 2:
			extend(a);
			break;
		case 3:
			pop(a);
			break;
		case 4:
			removefunc(a);
			break;
		case 5:
			indexx(a);
			break;
		case 6:
			insert(a);
			break;
		case 7:
			list_size(a);
			break;
		case 8:
			sort(a);
			break;
		case 9:
			reverse(a);
			break;
		case 10:
			count(a);
			break;
		case 11:
			check_value_in_list(a);
			break;
		case 12:
			add_values(a);
			break;

		case 13:
			exit(0);

		}
		printf("\nType 'again' to run the program once again \n");
		scanf("%s", string);
	}
	while (strcmp(string, "again") == 0);
	return 0;

}
